﻿using Daphne.GameShared;
using Daphne.Modularity.Commanding;
using Daphne.Modularity.UI;
using Daphne.SharedMetro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using System.Collections.Concurrent;

namespace Daphne.PagesMetro.ViewModels
{
    /// <summary>
    /// ViewModel for license page
    /// </summary>
    public class LicensesPageVM: ViewModelBase, IInitialization
    {
        /// <summary>
        /// Reference to the licence parser
        /// </summary>
        [Dependency]
        protected ILicensesParser Parser
        { get; set; }

        /// <summary>
        /// Selected library
        /// </summary>
        private LibraryRecord _selectedLibrary;

        /// <summary>
        /// Constructor
        /// </summary>
        public LicensesPageVM()
        {
            // init vars
            LibraryList = new ConcurrentBag<LibraryRecord>();

            // create commands
            GoBackCommand = new RelayCommand(() => { NotifyToLeavePage(); });
        }

        #region Binding Properties

        public RelayCommand GoBackCommand
        { get; set; }

        public ConcurrentBag<LibraryRecord> LibraryList
        { get; set; }


        public LibraryRecord SelectedLibrary
        {
            get
            {
                return _selectedLibrary;
            }
            set
            {
                _selectedLibrary = value;
                ShowLicense();
            }
        }

        #endregion

        /// <summary>
        /// Initialization of the parser
        /// TO-DO: do some error handling
        /// </summary>
        async public void OnInitialized()
        {
            try
            {
                // parse licence parse
                await Parser.Parse();
            }
            catch 
            {
            }

            // set binding properties
            foreach(var rec in Parser.LibraryRecords)
            {
                LibraryList.Add(rec);
            }


            // notify about the change
            // TO-DO: make better change in the notification of the list of libraries
            var saveList = LibraryList;
            LibraryList = null;
            RaisePropertyChanged(() => LibraryList);
            LibraryList = saveList;
            RaisePropertyChanged(() => LibraryList);
        }

        /// <summary>
        /// Sends notification to the view to leave this page
        /// </summary>
        private void NotifyToLeavePage()
        {
            // notify to go to the main page
            Messenger.Send<LeavePageMessage>(new LeavePageMessage());
        }

        /// <summary>
        /// Method for showing license
        /// </summary>
        async private void ShowLicense()
        {
            // if no selection or no license
            if(_selectedLibrary == null || string.IsNullOrWhiteSpace(_selectedLibrary.LicenseFile))
            {
                NotifyToChangeLicense(string.Empty);
                return;
            }

            // load the license file
            await Parser.LoadLicenseFileAsync(_selectedLibrary.LicenseFile);

            // notify
            NotifyToChangeLicense(Parser.LicenseFileContent);
        }

        /// <summary>
        /// Sends notification to the view to set the licence
        /// </summary>
        void NotifyToChangeLicense(string license)
        {
            Messenger.Send<SetLicenceMessage>(new SetLicenceMessage(license));
        }


    }
}
